//
//  AgreeView.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/4/13.
//

import SwiftUI
import FlowStacks

struct AgreeView: View {
    @EnvironmentObject var navigator: FlowNavigator<LoginStack>
    @Binding var isAgree: Bool
    var stackKey: String?
    var body: some View {
        VStack(alignment: .leading){
            HStack {
                Checkbox(isChecked: $isAgree, label: "I have readed and agreed")
                LinkText(text: "<User Agreement>") {
                    navigator.push(.webPage(url: USER_PROTOCOL))
                }
                Text("and").foregroundColor(AppColors.Gy3).font(.system(size: 14))
                LinkText(text: "<Privacy Agreement>") {
                    navigator.push(.webPage(url: PRIVACY_PROTOCOL))
                }
            }
            Text("unregistered accounts will automatically create Casdon accounts").foregroundColor(AppColors.Gy3).font(.system(size: 14))
                .padding(.leading, 25)
            
        }
    }
}

struct AgreementPopupOverlay: View {
    var onTapUserAgreement: () -> Void
    var onTapPrivacyPolicy: () -> Void
    var onDecline: () -> Void
    var onConfirm: () -> Void
    @State var show = false

    var body: some View {
        ZStack {
            // 半透明背景遮罩
            Color.black.opacity(0.4)
                .ignoresSafeArea()

            // 弹窗居中内容
            VStack(spacing: 0) {
                VStack(spacing: 8) {
                    Text("为保护您的合法权益，请先阅读并同意以下协议")
                        .foregroundColor(.gray)
                        .font(.body)
                        .multilineTextAlignment(.center)

                    HStack(spacing: 4) {
                        Button(action: onTapUserAgreement) {
                            Text("《用户协议》")
                                .foregroundColor(AppColors.Brand6)
                                .fontWeight(.semibold)
                        }
                        Text("和")
                            .foregroundColor(.gray)
                        Button(action: onTapPrivacyPolicy) {
                            Text("《隐私政策》")
                                .foregroundColor(AppColors.Brand6)
                                .fontWeight(.semibold)
                        }
                    }
                    .font(.subheadline)
                }
                .padding(.horizontal, 24)
                .padding(.top, 24)
                .padding(.bottom, 16)

                Divider()

                HStack(spacing: 0) {
                    Button(action:{show = false;  onDecline()}) {
                        Text("我再想想")
                            .foregroundColor(AppColors.Gy2)
                            .font(.system(size: 16))
                            .frame(maxWidth: .infinity, maxHeight: 44)
                    }
                    .contentShape(Rectangle())

                    Divider()

                    Button(action: {show = false; onConfirm()}) {
                        Text("同意")
                            .foregroundColor(AppColors.Gy1)
                            .font(.system(size: 16, weight: .bold))
                            .frame(maxWidth: .infinity, maxHeight: 44)
                    }
                    .contentShape(Rectangle())
                }
                .frame(height: 44)
            }
            .background(Color.white)
            .cornerRadius(12)
            .frame(maxWidth: 300)
            .shadow(radius: 10)
            
            
        }
    }
}


